package com.ajk.server.dao.provider;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;

import com.ajk.server.model.Suggest;
import com.ajk.server.model.query.SuggestQuery;
import com.ajk.server.utils.IntegerUtil;
/**
 * 意见反馈  <br>
 * t_suggest <br>
 * v1.0 2016-05-01
 * @see com.ajk.server.dao.SuggestMapper
 */
 public class SuggestProvider {
 
 	/**
 	* 新增 意见反馈
 	*/
 	public String insert(Suggest domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("insert into t_suggest(create_time,last_modify_time");
	 	if(domain.getUserId()!=null){
			sb.append(",user_id");
		}
	 	if(domain.getContent()!=null){
			sb.append(",content");
		}
		
		sb.append(") values ( now(),now() ");
		
	 	if(domain.getUserId()!=null){
			sb.append(",#{userId}");
		}
	 	if(domain.getContent()!=null){
			sb.append(",#{content}");
		}
		sb.append(")");
		
		return sb.toString();
	}

	/**
 	* 更新 意见反馈
 	*/
	public String update(Suggest domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("update t_suggest set last_modify_time = now() ");
		
		if(domain.getUserId()!=null){
			sb.append(",user_id=#{userId}");
		}
		if(domain.getContent()!=null){
			sb.append(",content=#{content}");
		}
		
		sb.append(" where id=#{id}");
		
		return sb.toString();
	}
	
	public String batchInsert(Map<String, List<Suggest>> map){
		StringBuffer sb = new StringBuffer();
		List<Suggest> suggests = map.get("list");
		sb.append("insert into t_suggest(create_time,last_modify_time,user_id,content) values");
	    MessageFormat messageFormat = new MessageFormat("("
	    		+ "now(),now()"
	    		+ ",#'{'list[{0}].userId}"
	    		+ ",#'{'list[{0}].content}"
	    		+")");
	    		
	    if(CollectionUtils.isNotEmpty(suggests)) {
			sb.append(messageFormat.format(new Integer[]{0}));
			for (int i = 1; i <  suggests.size(); i++) {
				sb.append(",").append(messageFormat.format(new Integer[]{i}));
			}
		}
		return sb.toString();
	}
	
	/**
 	*通过query 查询 意见反馈
 	*/
	public String findByQuery(SuggestQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select * from t_suggest where 1 = 1 ");

		if(IntegerUtil.isPositiveInteger(query.getUserId())){
			sb.append(" and user_id = #{userId}");
		}
		if(IntegerUtil.isPositiveInteger(query.getStartId())){
			sb.append(" and id>#{startId} order by id limit #{limit}");
		}else if(IntegerUtil.isPositiveInteger(query.getEndId())){
			sb.append(" and id<#{endId} order by id desc limit #{limit}");
		}else if(query.getStart()!=null && query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" order by id desc limit #{start},#{limit}");
		}else if(query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" order by id desc limit #{limit}");
		}
		return sb.toString();
	}
	
	public String countByQuery(SuggestQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(1) from t_suggest where 1 = 1 ");

		if(IntegerUtil.isPositiveInteger(query.getUserId())){
			sb.append(" and user_id = #{userId}");
		}
		return sb.toString();
	}
	
	/**
 	*通过ids 查询 意见反馈
 	*/
 	public String findByIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = map.get("list");
		sb.append("select * from t_suggest where ");
		if(ids==null || ids.isEmpty()){
			sb.append(" 1<2");
		}else{
			if(ids.size()==1){
				sb.append(" id=").append("#{list[0]}");
			}else{
				sb.append(" id in (").append("#{list[0]}");
				for(int i=1; i<ids.size() ; i++){
					sb.append(",").append("#{list[").append(i).append("]}");
				}
				sb.append(")");
			}
		}
		return sb.toString();
	}
	
 }	